/**
 * 
 */
package com.sci.web.action.datareportseason.enterprise_attachment;

import java.awt.Image;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

import javax.annotation.Resource;
import javax.imageio.ImageIO;

import org.apache.struts2.ServletActionContext;

import com.sci.web.action.util.BaseAction;
import com.sci.web.bean.Enterprise_Attachment;
import com.sci.web.dao.DateReportM_Enterprise_AttachmentDao;

/**
 * @author BaiFuyou
 * @date: Nov 8, 2011 4:42:29 PM
 * 
 * 上传扫描文件
 */
public class UploadEnterprise_AttachmentAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1566542495274514779L;
	@Resource
	private DateReportM_Enterprise_AttachmentDao dateReportM_Enterprise_AttachmentDao;
	private int uid;
	private String attachment_name;
	private String attachment_type;
	private String attachment_description;
	private String year;
	private int type;
	/**
	 * 对应的上传的文件
	 */
	private File imagefile;
	/**
	 * 文件类型
	 */
	private String imagefileContentType;
	/**
	 * 对应的文件名
	 */
	private String imagefileFileName;
	/**
	 * 保存路径
	 */
	private String savePath;
	private boolean success;
	private String msg;

	/**
	 * @return the success
	 */
	@com.googlecode.jsonplugin.annotations.JSON(serialize = false)
	public boolean isSuccess() {
		return success;
	}

	/**
	 * @param success
	 *            the success to set
	 */
	public void setSuccess(boolean success) {
		this.success = success;
	}

	/**
	 * @return the msg
	 */
	@com.googlecode.jsonplugin.annotations.JSON(serialize = false)
	public String getMsg() {
		return msg;
	}

	/**
	 * @param msg
	 *            the msg to set
	 */
	public void setMsg(String msg) {
		this.msg = msg;
	}

	/**
	 * @return the imagefile
	 */
	@com.googlecode.jsonplugin.annotations.JSON(serialize = false)
	public File getImagefile() {
		return imagefile;
	}

	/**
	 * @param imagefile
	 *            the imagefile to set
	 */
	public void setImagefile(File imagefile) {
		System.out.println(BaseAction.NONE + " =====imagefile === " + imagefile);
		this.imagefile = imagefile;
	}

	/**
	 * @return the imagefileContentType
	 */
	@com.googlecode.jsonplugin.annotations.JSON(serialize = false)
	public String getImagefileContentType() {
		return imagefileContentType;
	}

	/**
	 * @param imagefileContentType
	 *            the imagefileContentType to set
	 */
	public void setImagefileContentType(String imagefileContentType) {
		this.imagefileContentType = imagefileContentType;
	}

	/**
	 * @return the imagefileFileName
	 */
	@com.googlecode.jsonplugin.annotations.JSON(serialize = false)
	public String getImagefileFileName() {
		return imagefileFileName;
	}

	/**
	 * @param imagefileFileName
	 *            the imagefileFileName to set
	 */
	public void setImagefileFileName(String imagefileFileName) {
		this.imagefileFileName = imagefileFileName;
	}

	/**
	 * @return the savePath
	 */
	@com.googlecode.jsonplugin.annotations.JSON(serialize = false)
	public String getSavePath() {
		return ServletActionContext.getServletContext().getRealPath("/image/file");
	}

	/**
	 * @param savePath
	 *            the savePath to set
	 */
	public void setSavePath(String savePath) {

		this.savePath = "/image/file";
	}

	/**
	 * @return the uid
	 */
	@com.googlecode.jsonplugin.annotations.JSON(serialize = false)
	public int getUid() {
		return uid;
	}

	/**
	 * @param uid
	 *            the uid to set
	 */
	public void setUid(int uid) {
		this.uid = uid;
	}

	/**
	 * @return the attachment_name
	 */
	@com.googlecode.jsonplugin.annotations.JSON(serialize = false)
	public String getAttachment_name() {
		return attachment_name;
	}

	/**
	 * @param attachment_name
	 *            the attachment_name to set
	 */
	public void setAttachment_name(String attachment_name) {
		this.attachment_name = attachment_name;
	}

	/**
	 * @return the attachment_type
	 */
	@com.googlecode.jsonplugin.annotations.JSON(serialize = false)
	public String getAttachment_type() {

		return attachment_type;
	}

	/**
	 * @param attachment_type
	 *            the attachment_type to set
	 */
	public void setAttachment_type(String attachment_type) {
		this.attachment_type = attachment_type;
	}

	/**
	 * @return the attachment_description
	 */
	@com.googlecode.jsonplugin.annotations.JSON(serialize = false)
	public String getAttachment_description() {
		return attachment_description;
	}

	/**
	 * @param attachment_description
	 *            the attachment_description to set
	 */
	public void setAttachment_description(String attachment_description) {
		this.attachment_description = attachment_description;
	}

	@Override
	public String execute() throws Exception {
		// TODO Auto-generated method stub
		super.setContentType(super.HTML);
		/**
		 * 获取文件名
		 */
		String filename = this.getImagefileFileName();
		int a_type = -1;
		/**
		 * 文件类型
		 */
		String imagetype = filename.substring(filename.lastIndexOf(".") + 1, filename.length());

		if (!imagetype.equalsIgnoreCase("jpg")) {
			this.success = false;
			this.msg = "上传的文件格式不对，请选择JPG格式文件上传";
			super.outPrint("{success:'" + this.success + "', msg:'" + this.msg + "'}");
			return NONE;
		}

		/**
		 * 文件输出
		 */
		FileOutputStream fos = null;
		/**
		 * 文件输入
		 */
		FileInputStream fis = null;
		fis = new FileInputStream(this.getImagefile());
		// File file = new File(this.getImagefile().toString());
		Image src = ImageIO.read(this.getImagefile());
		int height = src.getHeight(null);
		int width = src.getWidth(null);
		String name = System.currentTimeMillis() + "_" + (new Random()).nextInt(100000) + ".jpg";

		String fname = this.getSavePath() + File.separator + name;
		String filerealname = "image"+File.separator+"file"+File.separator+name;
		// System.out.println(name);
		fos = new FileOutputStream(fname);
		byte[] buffer = new byte[1024];
		int length = 0;
		try {
			while ((length = fis.read(buffer)) > 0) {
				try {
					fos.write(buffer, 0, length);
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			this.success = true;
			this.msg = "上传成功";
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			this.success = false;
			this.msg = "上传失败";
		}
		if (!this.success) {
			super.outPrint("{success:" + this.success + ", msg:'" + "文件读取错误" + "'}");
			return super.NONE;
		}
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd ");
		String nowDate = sdf.format(new Date()).toString();	
		Enterprise_Attachment ex = new Enterprise_Attachment();
		ex.setAttachment_check_pass(3);
		ex.setAttachment_create_time(nowDate);
		ex.setAttachment_description(attachment_description);
		ex.setAttachment_file_sources(filerealname);
		ex.setAttachment_file_width(width);
		ex.setAttachment_file_heigth(height);
		ex.setAttachment_id(0);
		ex.setAttachment_is_new(0);
		ex.setAttachment_last_edit_time(null);
		ex.setAttachment_name(attachment_name);
		ex.setAttachment_type(getCodeByName(attachment_type));
		ex.setAttachment_type_sb(type);
		ex.setAttachment_year(year);
		ex.setEnterprise_id(dateReportM_Enterprise_AttachmentDao.getEnterprsieIdByUid(uid));
		// Interface_Image ii = new Interface_Image();
		// ii.setImage_url("interface_image"+File.separator+name);
		// ii.setInterface_module_id(face_id);
		// ii.setHeight(height);
		// ii.setWidth(width);
		// boolean imagesafe = minterface.insertNewInterfaceImage(ii);
		if (dateReportM_Enterprise_AttachmentDao.insertNewEnterprise_Attachmen(ex)) {
			this.success = true;
			this.msg = "简图上传成功";
		} else {
			this.success = false;
			this.msg = "简图上传失败";
		}
		super.outPrint("{success:" + this.success + ", msg:'" + this.msg + "'}");
		return NONE;
	}

	public int getCodeByName(String code) {
		if (code.equals("企业营业执照")) {
			return 1;
		} else if (code.equals("年度财务审计报告")) {
			return 2;
		} else if (code.equals("各类认证证书")) {
			return 3;
		} else if (code.equals("品牌认证证书")) {
			return 4;
		} else if (code.equals("研发机构认证证书")) {
			return 5;
		} else if (code.equals("研发人员名录")) {
			return 6;
		} else if (code.equals("承担科技项目立项文件")) {
			return 7;
		} else if (code.equals("省部级科技奖励文件")) {
			return 8;
		} else if (code.equals("专利授权证书、受理通知书")) {
			return 9;
		} else if (code.equals("主持（参与）指定标准证明")) {
			return 10;
		} else if (code.equals("管理制度")) {
			return 11;
		} else if (code.equals("各项荣誉证明")) {
			return 12;
		} else if (code.equals("其他各项证明材料")) {
			return 13;
		} else {
			return 0;
		}

	}

	/**
	 * @return the year
	 */

	@com.googlecode.jsonplugin.annotations.JSON(serialize = false)
	public String getYear() {
		return year;
	}

	/**
	 * @param year
	 *            the year to set
	 */
	public void setYear(String year) {
		this.year = year;
	}

	/**
	 * @return the type
	 */
	@com.googlecode.jsonplugin.annotations.JSON(serialize = false)
	public int getType() {
		return type;
	}

	/**
	 * @param type
	 *            the type to set
	 */
	public void setType(int type) {
		this.type = type;
	}

}
